/*
 * ODO: Real-Time In-Memory ORM/DBMS Grid software
 * Copyright (c) 2008, Michael Oksenenko
 * All rights reserved.
 */
//----------------------------------------------------------------------------
#include "OsConfig.hpp"
#if ODO_OS_VISTA == ODO_CHOSEN_OS
//----------------------------------------------------------------------------
#include "ProcessVista.hpp"

#include "OsComponentFactory.hpp"
#include "windows.h"
#include <string>
#include <iostream>

using namespace odo;

std::wstring ProcessVista::getCurrentProcessFullname() {
	const size_t PATH_MAX_SIZE = static_cast<size_t>(STRING_BUFFER_SIZE);
	wchar_t fullnameBuffer[1 + PATH_MAX_SIZE];
	DWORD retCode = GetModuleFileNameW(
		static_cast<HMODULE>(0),			// hModule
		static_cast<LPTSTR>(fullnameBuffer),// lpFilename
		static_cast<DWORD>(PATH_MAX_SIZE)	// nSize
	);
	if (0 == retCode) {
		OsComponentFactory::getError()->printLast();
		std::wcerr << L"FAIL get current process fullname." << std::endl;
		return std::wstring(L"");
	}
	if (PATH_MAX_SIZE == retCode) {
		OsComponentFactory::getError()->printLast();
		std::wcerr << L"FAIL get current process fullname; "
			<< "small buffer size: " << PATH_MAX_SIZE << std::endl;
	}
	fullnameBuffer[PATH_MAX_SIZE] = L'\0'; // guard
	return std::wstring(fullnameBuffer);
}

std::wstring ProcessVista::getCurrentProcessPath() {
	std::wstring path = getCurrentProcessFullname();
	size_t eraseFrom = 1 + path.find_last_of(L'\\');
	if (path.size() >= eraseFrom) {
		path.erase(eraseFrom);
	}
	return path;
}

ProcessVista::~ProcessVista() {
	// void
}

ProcessVista::ProcessVista() {
	// void
}
//----------------------------------------------------------------------------
#endif /* ODO_OS_VISTA */
//----------------------------------------------------------------------------
